from django.shortcuts import render, get_object_or_404

from wiki.models import Article, Category, Tag

import markdown

from django.views.generic import ListView
from django.utils.safestring import mark_safe


# Create your views here.
def index(request):
    """
    博客首页
    :param request:
    :return:
    """
    article_list = Article.objects.order_by('-publish')[:5]  # 最近5篇文章
    #article_list = Article.objects.all()  # 最近5篇文章
    #
    tag_all = [tag for tag in Tag.objects.all()]

    return render(request, 'wiki/index.html', locals())


def detail(request, pk):
    """
    博文详情
    """
    article = get_object_or_404(Article, pk=pk)
    # ua = request.META.get('HTTP_USER_AGENT')
    # ipaddr = request.META['REMOTE_ADDR']

    article.viewed()  # 阅读量统计

    md = markdown.Markdown(extensions=[
        'markdown.extensions.extra',
        'markdown.extensions.codehilite',
        'markdown.extensions.toc',
    ])

    article.body = md.convert(article.body.replace("\r\n", '  \n'))
    toc = md.toc

    tag_all = [tag for tag in Tag.objects.all()]

    detail_tag = Article.objects.get(pk=pk).tags.all()

    if request.user.is_authenticated:
        user_login = True

    context = locals()
    return render(request, 'wiki/detail.html', context)


def search(request):
    key = request.GET.get('key')
    error_msg = ''

    tag_all = [tag for tag in Tag.objects.all()]

    if not key:
        error_msg = "请输入关键词"
        return render(request, 'wiki/index.html', {'error_msg': error_msg})

    elif request.user.is_authenticated:  # 判断是否登录
        article_list = Article.objects.filter(title__icontains=key)
        return render(request, 'wiki/index.html', {'error_msg': error_msg,
                                              "tag_all": tag_all,
                                              "article_list": article_list, "key": key})
    else:
        error_msg = mark_safe('''<div class="alert alert-info">
				 <button type="button" class="close" data-dismiss="alert">×</button>
				<h4>
					提示!
				</h4> <strong>搜索失败!</strong> 请先登录 ...
			</div>''')
        return render(request, 'wiki/index.html', {'error_msg': error_msg})


def articles(request, pk):
    """
    博客列表页面
    :param request:
    :param pk:
    :return:
    """
    pk = int(pk)
    if pk:
        category_object = get_object_or_404(Category, pk=pk)
        category = category_object.name
        article_list = Article.objects.filter(category_id=pk)
    else:
        # pk为0时表示全部
        article_list = Article.objects.all()  # 获取全部文章
        category = ''
    return render(request, 'wiki/articles.html', {"article_list": article_list,
                                                  "category": category,
                                                  })


def archive(request, year, month):
    """
    归档
    :param request:
    :param year:
    :param month:
    :return:
    """
    article_list = Article.objects.filter(publish__year=year, publish__month=month).order_by('-publish')
    return render(request, 'wiki/archive.html', context={"article_list": article_list})


class TagView(ListView):
    model = Tag
    context_object_name = 'tags'
    template_name = 'wiki/tags.html'


class CategoryView(ListView):
    model = Category
    context_object_name = 'categories'
    template_name = 'wiki/category.html'


class ArchiveView(ListView):
    model = Article
    context_object_name = 'article_list'
    template_name = 'wiki/archive.html'


def tag(request, name):
    """
    标签
    :param request:
    :param name
    :return:
    """
    tag_all = [tag for tag in Tag.objects.all()]
    article_list = Article.objects.filter(tags__name=name)
    return render(request, 'wiki/index.html', {"article_list": article_list,
                                               "tag_all": tag_all
                                               })


def category(request, pk):
    # 记得在开始部分导入 Category 类

    tag_all = [tag for tag in Tag.objects.all()]

    cate = get_object_or_404(Category, pk=pk)
    article_list = Article.objects.filter(category=cate).order_by('-publish')
    return render(request, 'wiki/index.html', context={'article_list': article_list,
                                                       "tag_all": tag_all,
                                                       })
